#include "N1BinStream.h"
#include "N1Lib.h"


N1Stream& N1Stream::operator&(int8 &v)
{
    if(m_isWriteStream)
    {
        write(&v, sizeof(v));
    }
    else
    {
        read(&v, sizeof(v));
    }

    return *this;
}

N1Stream& N1Stream::operator&(uint8 &v)
{
    if(m_isWriteStream)
    {
        write(&v, sizeof(v));
    }
    else
    {
        read(&v, sizeof(v));
    }

    return *this;
}

N1Stream& N1Stream::operator&(int16 &v)
{
    if(m_isWriteStream)
    {
        short _s = N1H2N16(v);
        write(&_s, sizeof(_s));
    }
    else
    {
        short _s;
        read(&_s, sizeof(_s));
        v =  N1N2H16(_s);
    }

    return *this;
}

N1Stream& N1Stream::operator&(uint16 &v)
{
    if(m_isWriteStream)
    {
        short _s = N1H2N16(v);
        write(&_s, sizeof(_s));
    }
    else
    {
        short _s;
        read(&_s, sizeof(_s));
        v =  N1N2H16(_s);
    }

    return *this;
}

N1Stream& N1Stream::operator&(int32 &v)
{
    if(m_isWriteStream)
    {
        short _s = N1H2N32(v);
        write(&_s, sizeof(_s));
    }
    else
    {
        short _s;
        read(&_s, sizeof(_s));
        v =  N1N2H32(_s);
    }

    return *this;
}

N1Stream& N1Stream::operator&(uint32 &v)
{
    if(m_isWriteStream)
    {
        short _s = N1H2N32(v);
        write(&_s, sizeof(_s));
    }
    else
    {
        short _s;
        read(&_s, sizeof(_s));
        v =  N1N2H32(_s);
    }

    return *this;
}

N1Stream& N1Stream::operator&(int64 &v)
{
    if(m_isWriteStream)
    {
        short _s = N1H2N64(v);
        write(&_s, sizeof(_s));
    }
    else
    {
        short _s;
        read(&_s, sizeof(_s));
        v =  N1N2H64(_s);
    }

    return *this;
}

N1Stream& N1Stream::operator&(uint64 &v)
{
    if(m_isWriteStream)
    {
        short _s = N1H2N64(v);
        write(&_s, sizeof(_s));
    }
    else
    {
        short _s;
        read(&_s, sizeof(_s));
        v =  N1N2H64(_s);
    }

    return *this;
}

N1Stream& N1Stream::operator&(float &v)
{
    if(m_isWriteStream)
    {
        write(&v, sizeof(v));
    }
    else
    {
        read(&v, sizeof(v));
    }

    return *this;
}

N1Stream& N1Stream::operator&(double &v)
{
    if(m_isWriteStream)
    {
        write(&v, sizeof(v));
    }
    else
    {
        read(&v, sizeof(v));
    }

    return *this;
}

N1Stream& N1Stream::at(void* ptr, uint32 size)
{
    if(m_isWriteStream)
    {
        write(ptr, size);
    }
    else
    {
        read(ptr, size);
    }

    return *this;
}
